/*******************************************************************************
FigureB1.do

This file uses data created in "Code\Data_setup\Rent_and_Population_Data.do" and 
creates binscatters of rents per square foot vs population density at both the
county and zip code levels.

Last updated: 6/3/2021
*******************************************************************************/

version 15
cd "C:\Plants_in_Space"
set more off
set type double
set varabbrev off
graph set window fontface "Times New Roman" 

// Specify the number of bins you want to use at each geography level in the 
// binscatter. Default is 100 for ZIP codes, 50 for counties (since there are 
// fewer counties than ZIP codes).
local zip_num_bins = 100
local county_num_bins = 50

*******************************ZIP CODE LEVEL DATA******************************
// Load in data created in "Code\Data_setup\Rent_and_Population_Data.do"
use "Data\Final\FigureB1\zip_code_rents_and_population_density.dta", clear
rename (rent_per_sq_ft pop_density) (rent_per_sq_ft_zip pop_density_zip)

// Binscatter of Zillow data against population density.
binscatter rent_per_sq_ft_zip pop_density_zip, nquantiles(`zip_num_bins') linetype(none) savedata("Data\Final\FigureB1\zip_code_rent_per_square_foot_vs_pop_density_binscatter") replace
import delimited "Data\Final\FigureB1\zip_code_rent_per_square_foot_vs_pop_density_binscatter.csv", clear
save "Data\Final\FigureB1\zip_code_rent_per_square_foot_vs_pop_density_binscatter.dta", replace

*****************************COUNTY LEVEL DATA**********************************
// Load in data created in "Code\Data_setup\Rent_and_Population_Data.do"
use "Data\Final\FigureB1\county_rents_and_population_density.dta", clear
rename (rent_per_sq_ft pop_density) (rent_per_sq_ft_county pop_density_county)

// Binscatter of Zillow data against population density.
binscatter rent_per_sq_ft_county pop_density_county, nquantiles(`county_num_bins') linetype(none) savedata("Data\Final\FigureB1\county_rent_per_square_foot_vs_pop_density_binscatter") replace
import delimited "Data\Final\FigureB1\county_rent_per_square_foot_vs_pop_density_binscatter.csv", clear
save "Data\Final\FigureB1\county_rent_per_square_foot_vs_pop_density_binscatter.dta", replace

****************************COMBINE INTO ONE FIGURE*****************************
use "Data\Final\FigureB1\zip_code_rent_per_square_foot_vs_pop_density_binscatter.dta", clear
merge 1:1 _n using "Data\Final\FigureB1\county_rent_per_square_foot_vs_pop_density_binscatter.dta", nogen

twoway (scatter rent_per_sq_ft_zip pop_density_zip, msymbol(smdiamond)) ///
	   (scatter rent_per_sq_ft_county pop_density_county, msymbol(smtriangle)), ///
	   xtitle("Population Per Square Mile", size(medium)) ///
	   ytitle("Rent Per Square Foot", size(medium)) ///
	   legend(order(1 "ZIP Code Level" 2 "County Level") cols(1) size(medium) ring(0) bplacement(seast)) ///
	   xscale(log) yscale(log) xlabel(1 10 100 1000 10000 50000, labsize(medium)) ylabel(1 1.5 2 2.5 3, labsize(medium) nogrid) ///
	   plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "Plots\FigureB1.png", width(1000) replace
